package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.LawsuitMapper;
import com.molichuxing.services.infrastructure.dto.request.create.LawsuitCreateDto;
import com.molichuxing.services.infrastructure.dto.response.LawsuitDto;
import com.molichuxing.services.infrastructure.dto.response.LawsuitListDto;
import com.molichuxing.services.infrastructure.service.LawsuitService;
import com.molichuxing.services.property.LawsuitStateEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @Author zoumingyu
 * @Date 2020年07月28日
 */
@Service("lawsuitService")
public class LawsuitServiceImpl implements LawsuitService {


    @Resource
    private LawsuitMapper lawsuitMapper;


    /**
     * 查询法务诉讼列表
     *
     * @param params   查询参数
     * @param pageNum  页数
     * @param pageSize 每页条数
     * @return
     */
    @Override
    public Paged<LawsuitListDto> getPaged(Map<String, Object> params, int pageNum, int pageSize) {
        Paged<LawsuitListDto> result = new Paged<>(null, 0, pageSize);
        // 诉讼开始时间-LocalDateTime
        params.put("lawsuitTimeStart", ObjectCastUtil.castLocalDateTimeStart(params.get("lawsuitTimeStart")));
        // 诉讼结束时间-LocalDateTime
        params.put("lawsuitTimeEnd", ObjectCastUtil.castLocalDateTimeEnd(params.get("lawsuitTimeEnd")));

        if (params.get("recycleCount") == null) {
            params.put("recycleCount", 10000);
        }
        result.setTotal(lawsuitMapper.getPagedCount(params));
        if (result.getTotal() > 0) {
            result.setList(lawsuitMapper.getPaged(params, pageNum, pageSize));
        }

        return result;
    }


    /**
     * 新增诉讼
     *
     * @param createDto 新增参数
     * @return boolean
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean create(LawsuitCreateDto createDto) {
        return lawsuitMapper.create(Convert.toLawsuitBean(createDto)) > 0;
    }

    /**
     * 完成诉讼
     *
     * @param orderCode 订单号
     * @return boolean
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public boolean modifyComplete(Long orderCode) {
        lawsuitMapper.modifyComplete(orderCode, LawsuitStateEnum.FINISH.getValue(), LocalDateTime.now());
        return true;
    }

    /**
     * 查询诉讼信息
     *
     * @param orderCode 订单号
     * @return LawsuitDto
     */
    @Override
    public LawsuitDto getLawsuit(Long orderCode) {
        return Convert.toLawsuitDto(lawsuitMapper.getLawsuitByOrderCode(orderCode));
    }

    /**
     * 查询诉讼数量
     *
     * @return int
     */
    @Override
    public int getLawsuitTotal() {
        return (int) lawsuitMapper.getLawsuitTotal();
    }
}
